package json_xml;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public final class XMLBeanUtils {
	/**
	 * 将Bean转换为XML
	 * 
	 * @param clazzMap
	 *            别名-类名映射Map
	 * @param bean
	 *            要转换为xml的bean对象
	 * @param imMap
	 *            要屏蔽的list中的根标签，如items，使其不显示
	 * @return XML字符串
	 */
	public static String bean2xml(Map<String, Class> clazzMap, Object bean, Map<Class, String> imMap) {
		XStream xstream = new XStream();
		if(clazzMap != null){
			for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next();
				xstream.alias(m.getKey(), m.getValue());
			}
		}
		if(imMap != null){
			for (Iterator it = imMap.entrySet().iterator(); it.hasNext();) {
				Map.Entry<Class, String> m = (Map.Entry<Class, String>) it.next();
				xstream.addImplicitCollection(m.getKey(), m.getValue());
			}
		}
		String xml = xstream.toXML(bean);
		return xml;
	}

	/**
	 * 将XML转换为Bean
	 * 
	 * @param clazzMap
	 *            别名-类名映射Map
	 * @param xml
	 *            要转换为bean对象的xml字符串
	 * @param imMap
	 *            要屏蔽的list中的根标签，如items，使其不显示
	 * @return Java Bean对象
	 */
	public static Object xml2Bean(Map<String, Class> clazzMap, String xml, Map<Class, String> imMap) {
		XStream xstream = new XStream(new DomDriver());
		if(clazzMap != null){
			for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next();
				xstream.alias(m.getKey(), m.getValue());
			}
		}
		if(imMap != null){
			for (Iterator it = imMap.entrySet().iterator(); it.hasNext();) {
				Map.Entry<Class, String> m = (Map.Entry<Class, String>) it.next();
				xstream.addImplicitCollection(m.getKey(), m.getValue());
			}
		}
		Object bean = xstream.fromXML(xml);
		return bean;
	}

	/**
	 * 获取XStream对象
	 * 
	 * @param clazzMap
	 *            别名-类名映射Map
	 * @return XStream对象
	 */
	public static XStream getXStreamObject(Map<String, Class> clazzMap) {
		XStream xstream = new XStream();
		for (Iterator it = clazzMap.entrySet().iterator(); it.hasNext();) {
			Map.Entry<String, Class> m = (Map.Entry<String, Class>) it.next();
			xstream.alias(m.getKey(), m.getValue());
		}
		return xstream;
	}
	
	public static void main(String[] args) {
		
		Map<String, Class> map = new HashMap<String, Class>(); 
		map.put("a", Person.class); 
		map.put("b", Addr.class); 
		
		Map<Class, String> imMap = new HashMap<Class, String>(); 
		imMap.put(Person.class, "addrs"); 
		
		
		Person bean = new Person();
		bean.setAge(1);
		bean.setName("a");
		List<Addr> list = new ArrayList<Addr>();
		Addr addr = new Addr();
		addr.setAd("addresssssss");
		list.add(addr);
		bean.setAddrs(list);

		String xml = bean2xml(map, bean , imMap);
		System.out.println(xml);

		Person bean2 = (Person)xml2Bean(map, xml, imMap);
		System.out.println(bean2.getAge());
		System.out.println(bean2.getName());
		System.out.println(bean2.getAddrs().get(0).getAd());
	}
}